package com.xx.config.security;

import com.xx.comm.R;
import io.swagger.v3.core.util.Json;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName: CustomAuthenticationEntryPoint
 * @Description:
 * @Author: 晓东√
 * @Date: 2024-12-18 11:22
 * @Version: 1.0
 **/
@Component
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {


    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        // 设置响应内容类型和状态码
        response.setContentType("application/json;charset=UTF-8");
        // 写入JSON格式的错误信息
        try (PrintWriter writer = response.getWriter()) {

            writer.print(Json.pretty(R.fail(HttpServletResponse.SC_UNAUTHORIZED,"未认证")));
        }
    }

}